package cn.mothin.action.system;

import org.springframework.beans.factory.annotation.Autowired;

import cn.mothin.frame.bean.ActionResult;
import cn.mothin.frame.core.annotation.Module;
import cn.mothin.frame.core.annotation.NeedLogon;
import cn.mothin.frame.core.annotation.NeedModule;
import cn.mothin.frame.core.annotation.NeedPermissionAs;
import cn.mothin.frame.core.annotation.NeedPermissionTemplate;
import cn.mothin.frame.core.annotation.OperationLink;
import cn.mothin.frame.core.base.BaseAction;
import cn.mothin.service.system.DictionaryService;
import cn.mothin.vo.system.DictionaryForm;

@SuppressWarnings("serial")
@Module(id = "5410030a04a5c1319c558890", name = "字典管理")
public class DictionaryAction extends BaseAction {
    private DictionaryForm dictionaryForm = new DictionaryForm();
    @Autowired
    private DictionaryService dictionaryService;

    @Override
    public Object getModel() {
        return dictionaryForm;
    }

    @NeedModule
    public String execute() {
        return SUCCESS;
    }

    /**
     * 查询字典类型列表
     * 
     * @return
     */
    @NeedModule
    public String queryTypes() {
        return this.renderJson(dictionaryService.queryTypes(dictionaryForm));
    }

    /**
     * 添加字典类型页面
     * 
     * @return
     */
    @OperationLink(link = "execute")
    @NeedPermissionTemplate(id = "5411072204a57f8316a4768e", template = NeedPermissionTemplate.CREATE)
    public String addType() {
        return "add_type";
    }

    /**
     * 保存新的字典类型
     * 
     * @return
     */
    @NeedPermissionAs("addType")
    public String saveForAddType() {
        return renderJson(dictionaryService.addType(dictionaryForm).toJson());
    }

    /**
     * 设置字典项页面
     * 
     * @return
     */
    @OperationLink(link = "execute", ordinal = 0, rowOperation = true)
    @NeedPermissionTemplate(id = "54110dff04a57f8316a47691", template = NeedPermissionTemplate.CONFIG)
    public String config() {
        int result = dictionaryService.queryByTypeId(dictionaryForm);
        if (result == 0) {
            return "config";
        } else if (result == 1) {
            setLastError("该字典类型不存在！");
            return ERROR;
        } else if (result == 2) {
            setLastError("该字典类型已删除！");
            return ERROR;
        }
        return ERROR;
    }

    @NeedPermissionAs("config")
    public String queryItems() {
        return renderJson(dictionaryService.queryItems(dictionaryForm));
    }

    @OperationLink(link = "config", ordinal = 0)
    @NeedPermissionTemplate(id = "5411387804a57f8316a47692", template = NeedPermissionTemplate.CREATE, parent = "config")
    public String addItem() {
        return "add_item";
    }

    @NeedPermissionAs("addItem")
    public String saveForAddItem() {
        return renderJson(dictionaryService.addItem(dictionaryForm).toJson());
    }

    @OperationLink(link = "config", ordinal = 1, rowOperation = true)
    @NeedPermissionTemplate(id = "541164a504a57f8316a47695", template = NeedPermissionTemplate.CREATE, parent = "config")
    public String addChildItem() {
        return "add_childItem";
    }

    @NeedPermissionAs("addChildItem")
    public String saveForChildAddItem() {
        return renderJson(dictionaryService.addItem(dictionaryForm).toJson());
    }

    @OperationLink(link = "config", ordinal = 2, rowOperation = true)
    @NeedPermissionTemplate(id = "541138d904a57f8316a47693", template = NeedPermissionTemplate.UPDATE, parent = "config")
    public String updateItem() {
        int result = dictionaryService.queryItemById(dictionaryForm);
        if (result == 0) {
            return "update_item";
        } else if (result == 1) {
            setLastError("该字典项不存在！");
            return ERROR;
        } else if (result == 2) {
            setLastError("该字典项已删除！");
            return ERROR;
        }
        return ERROR;
    }

    @NeedPermissionAs("updateItem")
    public String saveForUpdateItem() {
        ActionResult actionResult = ActionResult.getInstance();
        int result = dictionaryService.updateItem(dictionaryForm);
        if (result == 1) {
            actionResult.setOk(false);
            actionResult.setMessage("指定的字典项不存在！");
        } else if (result == 2) {
            actionResult.setOk(false);
            actionResult.setMessage("此字典项名称已存在！");
        }
        return renderJson(actionResult.toJson());
    }

    @OperationLink(link = "config", ordinal = 3, rowOperation = true)
    @NeedPermissionTemplate(id = "541138f804a57f8316a47694", template = NeedPermissionTemplate.DELETE, parent = "config")
    public String deleteItem() {
        ActionResult actionResult = ActionResult.getInstance();
        int result = dictionaryService.deleteItem(dictionaryForm);
        if (result == 1) {
            actionResult.setOk(false);
            actionResult.setMessage("指定的字典项不存在！");
        }
        return renderJson(actionResult.toJson());
    }

    /**
     * 修改字典类型
     * 
     * @return
     */
    @OperationLink(link = "execute", ordinal = 1, rowOperation = true)
    @NeedPermissionTemplate(id = "5411077804a57f8316a4768f", template = NeedPermissionTemplate.UPDATE)
    public String updateType() {
        int result = dictionaryService.queryByTypeId(dictionaryForm);
        if (result == 0) {
            return "update_type";
        } else if (result == 1) {
            setLastError("该字典类型不存在！");
            return ERROR;
        } else if (result == 2) {
            setLastError("该字典类型已删除！");
            return ERROR;
        }
        return ERROR;
    }

    /**
     * 保存修改信息 0：正常保存成功 1：指定的类型不存在 2：名称重复
     * 
     * @return
     */
    @NeedPermissionAs("updateType")
    public String saveForUpdateType() {
        ActionResult actionResult = ActionResult.getInstance();
        int result = dictionaryService.updateType(dictionaryForm);
        if (result == 1) {
            actionResult.setOk(false);
            actionResult.setMessage("指定的字典类型不存在！");
        } else if (result == 2) {
            actionResult.setOk(false);
            actionResult.setMessage("此字典类型名称已存在！");
        }
        return renderJson(actionResult.toJson());
    }

    /**
     * 删除指定字典类型
     * 
     * @return
     */
    @OperationLink(link = "execute", ordinal = 2, rowOperation = true)
    @NeedPermissionTemplate(id = "541107e904a57f8316a47690", template = NeedPermissionTemplate.DELETE)
    public String deleteType() {
        ActionResult actionResult = ActionResult.getInstance();
        int result = dictionaryService.deleteType(dictionaryForm);
        if (result == 1) {
            actionResult.setOk(false);
            actionResult.setMessage("指定的字典类型不存在！");
        }
        return renderJson(actionResult.toJson());
    }

    /**
     * 字典选择控件数据获取
     * @return
     */
    @NeedLogon
    public String queryForControl(){
        return renderJson(dictionaryService.queryForControl(dictionaryForm).toJson());
    }
}
